package at.fhooe.mcm12.java.serializer;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class TextWriter extends AWriter implements Runnable {
	public TextWriter() {
		m_fileExt = ".txt";
		m_worker = new Thread(this);
		start();
	}

	public void write(WriterQueueElement elem) {
		String file = elem.getFile();
		try {
			file += m_fileExt;
			File f = new File(file);
			final File parent_directory = f.getParentFile();

			if (null != parent_directory) {
				parent_directory.mkdirs();
			}

			PrintWriter out = new PrintWriter(new BufferedWriter(
					new FileWriter(f, true)));
			for (int i = 0; i < elem.getValues().length; i++) {
				if (i > 0) {
					out.print(";");
				}
				out.print(elem.getTags()[i]);
				out.print(":");
				out.print(elem.getValues()[i]);

			}
			out.print("\n");
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
